package com.example.pingze.controller;

import com.example.pingze.entity.PoetInfo;
import com.example.pingze.entity.PoetUserCollect;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.PoetInfoRepository;
import com.example.pingze.service.PoetUserCollectService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/poetUserCollect")
@CrossOrigin(origins = "*")
public class PoetUserCollectController {
    @Autowired
    private PoetUserCollectService service;
    @Autowired
    private PoetInfoRepository poetInfoRepository;
    //获取name和account，添加到诗词收藏表 ~ 返回消息
    @PostMapping("/collect")
    public Result<String> collect(@RequestParam String account,
                                  @RequestParam String name){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(name == null){
            throw new PingZeException("诗人名为空");
        }
        PoetUserCollect find = service.findPoetUserCollectByAccountAndPoetName(account, name);
        if(find == null){
            PoetUserCollect collect = new PoetUserCollect();
            collect.setAccount(account);
            collect.setPoetName(name);
            collect.setId(UUID.randomUUID().toString());
            service.collect(collect);
        }else{
            throw new PingZeException("该诗人已被收藏");
        }

        return ResultVOUtils.success("收藏成功");
    }
    //获取name和account，将用户的诗词收藏移除 ~ 返回消息
    @PostMapping("/noCollect")
    public Result<String> noCollect(@RequestParam String account,
                                    @RequestParam String name){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(name == null){
            throw new PingZeException("诗人名为空");
        }
        PoetUserCollect collect = new PoetUserCollect();
        collect.setAccount(account);
        collect.setPoetName(name);
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        List<PoetUserCollect> find = service.findPoetUserCollect(collect);
        if(find != null){
            for (PoetUserCollect list : find){
                service.delete(list);
            }
            return ResultVOUtils.success("取消收藏成功");
        }else{
            throw new PingZeException("用户的诗人信息未收藏");
        }
    }
    //通过account展示诗人列表
    @PostMapping("/getPoetList")
    public Result<List<PoetInfo>> getPoetList(@RequestParam String account){
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        List<PoetUserCollect> poetUserCollectByAccount = service.findPoetUserCollectByAccount(account);
        if(poetUserCollectByAccount == null){
            throw new PingZeException("该用户该未收藏");
        }
        List<PoetInfo> poetInfoList = new ArrayList<>();
        //遍历查询出来的收藏信息
        for(PoetUserCollect poetUserCollect : poetUserCollectByAccount){
            String name = poetUserCollect.getPoetName();
            if(name != null){
                PoetInfo poetInfo = poetInfoRepository.findPoetInfoByPoetName(name);
                if(poetInfo != null){
                    poetInfoList.add(poetInfo);
                }
            }
        }
        return ResultVOUtils.result(poetInfoList);
    }
    //查询，收藏信息是否在收藏表，如果不在，则返回0
    @PostMapping("/getStatus")
    public Result<String> getStatus(@RequestParam String account,
                                    @RequestParam String name){
        String status = "";
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(name == null){
            throw new PingZeException("用户账号为空");
        }
        PoetUserCollect collect = service.findPoetUserCollectByAccountAndPoetName(account, name);
        if(collect == null){
            status = "no";
        }else{
            status = "ok";
        }
        return ResultVOUtils.success(status);
    }
    /**
     * 后台管理系统
     */
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<PoetUserCollect>> getList(
            PoetUserCollect poetUserCollect,
            @RequestParam(name = "pageCode", required = false, defaultValue = "1") Integer pageCode,
            @RequestParam(name = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order accountOrder = Sort.Order.asc("account");
        Sort sort = Sort.by(accountOrder);
        Pageable pageable = PageRequest.of(pageCode - 1, pageSize, sort);
        Page page = service.getList(poetUserCollect, pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<PoetUserCollect> add(String account, String poetName){
        PoetUserCollect add = service.add(account,poetName);
        return ResultVOUtils.result(add);
    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<PoetUserCollect> edit(String id, String account, String poetName){
        PoetUserCollect edit = service.edit(id, account, poetName);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null){
            throw new PingZeException("id为空");
        }
        PoetUserCollect find = service.findPoetUserCollectById(id);
        if(find != null){
            service.delete(find);
        }
        return ResultVOUtils.success("delete successful");
    }
}
